/********************************************************************
	created:	2008/11/10
	created:	10:11:2008   22:43
	filename: 	GDIPlusLib.h
	author:		Balon Fan
	
	purpose:	
*********************************************************************/


#pragma once


#include <GdiPlus.h>





	//////////////////////////////////////////////////////////////////////////
	// CGDIPlusInitializer

	class CGDIPlusInitializer
	{
	public:
		CGDIPlusInitializer();
		~CGDIPlusInitializer();

	protected:
		ULONG_PTR m_gdiplusToken;
	};


	//////////////////////////////////////////////////////////////////////////
	// CBufferGraphics & helper

	class CBufferGraphics
	{
	public:
		CBufferGraphics(Gdiplus::Graphics* pGraphics);

		~CBufferGraphics();

	public:
		Gdiplus::Graphics* GetGraphics()
		{
			return m_pBufferGraphics;
		}

	public:
		Gdiplus::Bitmap* m_pBitmap;
		Gdiplus::Rect    m_rcDest;
		Gdiplus::Graphics* m_pOriginalGraphics;
		Gdiplus::Graphics* m_pBufferGraphics;
	};


	class CEasyRect : public Gdiplus::Rect
	{
	public:
		CEasyRect(CRect rcMFC)
			: Gdiplus::Rect(rcMFC.left, rcMFC.top, rcMFC.Width(), rcMFC.Height())
		{
		};

		operator CRect()
		{
			return CRect(X, Y, X + Width, Y + Height);
		}
	};

	class CEasyRectF : public Gdiplus::RectF
	{
	public:
		CEasyRectF(CRect rcMFC)
			: Gdiplus::RectF(
				(Gdiplus::REAL)(rcMFC.left),
				(Gdiplus::REAL)(rcMFC.top),
				(Gdiplus::REAL)(rcMFC.Width()),
				(Gdiplus::REAL)(rcMFC.Height()))
		{
		};

		operator CRect()
		{
			return CRect((int)(X), (int)(Y), (int)(X + Width), (int)(Y + Height));
		}
	};


	bool Grayscale(Gdiplus::Bitmap* pBitmap);

	class SuperStringFormat
	{
	public:
		SuperStringFormat(INT nLineSpacing = 0)
			: m_nLineSpacing(nLineSpacing)
		{
		}

	public:
		INT  GetLineSpacing() const { return m_nLineSpacing; }
		void SetLineSpacing(INT nLineSpacing) { m_nLineSpacing = nLineSpacing; }

	private:
		INT m_nLineSpacing;
	};

	Gdiplus::Status SuperMeasureString(
		IN Gdiplus::Graphics           * pGraphics,
		IN const WCHAR                 * string,
		IN INT                           length,
		IN const Gdiplus::Font         * font,
		IN const Gdiplus::RectF        & layoutRect,
		IN const Gdiplus::StringFormat * stringFormat,
		IN const SuperStringFormat     * superFormat,
		OUT Gdiplus::RectF             * boundingBox,
		OUT INT                        * codepointsFitted = 0,
		OUT INT                        * linesFilled      = 0
		);

	Gdiplus::Status SuperDrawString(
		IN Gdiplus::Graphics           * pGraphics,
		IN const WCHAR                 * string,
		IN INT                           length,
		IN const Gdiplus::Font         * font,
		IN const Gdiplus::RectF        & layoutRect,
		IN const Gdiplus::StringFormat * stringFormat,
		IN const Gdiplus::Brush        * brush,
		IN const SuperStringFormat     * superFormat
		);


